//获取contentid
function getContentId(){
	var article_link=window.location.href;
	articleId=article_link.split('contentId=');
	articleId=articleId[1].substring(0,12);
	return articleId;
}

 
//获取spm
function getSpm(){
	var article_link=window.location.href;
    str = article_link.match(/business_spm\=(\S*?)\&/)[1];
	return str;
}

//获取淘宝sign及其对象
function makeTaobaoSignObj(data = ''){
	let json_data = JSON.stringify(data);
	//固定的appkey
	let appkey = 12574478;
	//取到当前cookie
	let ck = document.cookie;
	//取到当前时间戳
	let timestamp = (new Date()).getTime();
	//获取token
	let token = getToken(ck);
	let signParams = token+"&"+String(timestamp)+"&"+appkey+"&"+json_data;
	let sign = makeSign(signParams);
	
	return {
		json_data:json_data,
		appkey:appkey,
		timestamp:timestamp,
		token:token,
		sign:sign,
	}
}

//获取有好货请求地址
function getHhUrl(content_id){
	//获取文章id
	articleId = content_id;
	data = {"contentId": articleId, "source": "daren", "type": "h5", "params": "","business_spm": "", "track_params": ""};
	signObj = makeTaobaoSignObj(data);
	timestamp = signObj.timestamp;
	appkey = signObj.appkey;
	sign = signObj.sign;
	json_data = signObj.json_data;
	url = "https://h5api.m.taobao.com/h5/mtop.taobao.beehive.detail.contentservicenewv2/1.0/?jsv=2.5.1&appKey=" + appkey + 
	  "&t=" + String(timestamp) + "&sign=" + sign + "&api=mtop.taobao.beehive.detail.contentservicenewv2&v=1.0" +
	  "&AntiCreep=true&AntiFlood=true&preventFallback=true&type=json&dataType=json&data=" + encodeURI(json_data);
	return url;
}

//相关推荐请求地址
function getContentIdUrl(){
	//获取文章id
	articleId = getContentId();
	spm = getSpm();
	console.log(spm)
	params = `{\"business_spm\":\"${spm}\"}`;
	data = {"contentId":articleId,"type":"weex","source":"youhh_tuji","frontModuleName":"recommendContent","params":params};
	signObj = makeTaobaoSignObj(data);
	timestamp = signObj.timestamp;
	appkey = signObj.appkey;
	sign = signObj.sign;
	json_data = signObj.json_data;
	url = "https://h5api.m.taobao.com/h5/mtop.taobao.beehive.detail.contentrecommendservice/1.0/?jsv=2.5.1&appKey=" + appkey + 
	  "&t=" + String(timestamp) + "&sign=" + sign + "&api=mtop.taobao.beehive.detail.contentservicenewv2&v=1.0" +
	  "&AntiCreep=true&AntiFlood=true&preventFallback=true&type=json&dataType=json&data=" + encodeURI(json_data);
	return url;
}

	

//获取商品详情地址
function getProductUrl(id = ''){	
	ex = `{\"id\":\"${id}\"}`;
	data = {"id": id, "itemNumId": id, "exParams":ex , "detail_v":"8.0.0","utdid":"1"};		
	signObj = makeTaobaoSignObj(data);
	timestamp = signObj.timestamp;
	appkey = signObj.appkey;
	sign = signObj.sign;
    url = "https://h5api.m.taobao.com/h5/mtop.taobao.detail.getdetail/6.0/?jsv=2.5.1&appKey=" + appkey + 
	 	  "&t=" + String(timestamp) + "&sign=" + sign + "&api=mtop.taobao.baichuan.smb.get&v=1.0&isSec=0&ecode=0&AntiFlood=true&AntiCreep=true" +
		"&H5Request=true&ttid=2018@taobao_h5_9.9.9&type=originaljson&dataType=json&data=" + "%7B%22id%22%3A%22"+id+"%22%2C%22itemNumId%22%3A%22"+id+
		"%22%2C%22exParams%22%3A%22%7B%5C%22id%5C%22%3A%5C%22"+id+"%5C%22%7D%22%2C%22detail_v%22%3A%228.0.0%22%2C%22utdid%22%3A%221%22%7D";
	
	return url;	
}



//获取商品详情图
function getProductDescUrl(id = ''){
	data = {"id": id, "type": 0};		
	signObj = makeTaobaoSignObj(data);
	timestamp = signObj.timestamp;
	appkey = signObj.appkey;
	sign = signObj.sign;
	url = "https://h5api.m.taobao.com/h5/mtop.taobao.detail.getdesc/6.0/?jsv=2.4.11&appKey=12574478&t="+String(timestamp)+
		"&sign="+sign+"&api=mtop.taobao.detail.getdesc&v=6.0&type=json&dataType=json&timeout=20000&data="+
		"%7B%22id%22%3A%22"+id+"%22%2C%22type%22%3A%220%22%2C%22f%22%3A%22TB1pvkIcBWD3KVjSZFs8qwqkpla%22%7D";
	
	return url;
}

//获取有好货文章原始数据
function originArticleData(content_id){
	let getUrl = getHhUrl(content_id);
	result = '';
	$.ajax({
		url:getUrl,
		type:"get",
		async:false,
		success:function(data){
			console.log(data);
			if(data.hasOwnProperty('data')){
				data = data.data;
				if(data.hasOwnProperty('models')){
					result = data;
				}
			}
		},
		error:function(){
			alert('获取有好货文章原始数据出错');
		}
	})	
	
	return result;
}

//获取商品详情图片
function originProductDescImages(id = ''){
	let getUrl = getProductDescUrl(id);
	result = [];
	$.ajax({
		url:getUrl,
		type:"get",
		async:false,
		
		success:function(data){
			if(data != undefined){
				if(data.hasOwnProperty('data')){
					data = data.data;
					if(data.hasOwnProperty('wdescContent')){
						wdescContent = data.wdescContent;
						if(wdescContent.hasOwnProperty('pages')){
							pages = wdescContent.pages;
							if(pages.length > 0){
								for(page of pages){
									if(page.indexOf('size') != -1 && page.indexOf('gif') == -1){
										img = delHtmlTag(page);
										result.push(img);
									}
								}
							}
						}
					}
				}
			}
		},
		error:function(){
			alert('获取商品详情图片出错');
		}
	})	
	
	return result;
}


//获取商品信息
function originProduct(id = ''){
	let getUrl = getProductUrl(id);
	result = {};
	$.ajax({
		url:getUrl,
		type:"get",
		async:false,
		success:function(data){
			if(data.hasOwnProperty('data')){
				data = data.data;
				if(data.hasOwnProperty('item')){
					result = data.item;
				}
			}
		},
		error:function(){
			layer.msg('获取商品信息出错');
		}
	})	
	
	return result;
}

//有好货数据抛送
function hhDataSubmit(content_id,data){
	result = '';
	$.ajax({
		url:'https://dd.youdnr.com/api/mate/store',
		type:"post",
		async:false,
		 data:{
			 content_id:content_id,
			 data:data,
			 platform:0
		 },
		success:function(data){
			result = {
				code: data.code,
				msg:data.msg
			}
		},
		error:function(){
			layer.msg('有好货数据抛送异常');
		}
	})
	return result;
}



//有好货id获取 获取需要采集的content_id
function hhContentIdGet(){
	let getUrl = getContentIdUrl();
	res = [];
	$.ajax({
		url:getUrl,
		type:"get",
		async:false,
		success:function(data){
			if(data.hasOwnProperty('data')){
				data = data.data
				if(data){
					if(data.hasOwnProperty('result')){
						result = data.result;
						if(result){
							for(item of result){
								if(item.readCount > 50){
									res.push(item.id);
								}
							}
						}
					}
				}			
			}
		},
		error:function(){
			layer.msg('获取额外id出错');
		}
	})	
	
	return res;
}
//content_id数据抛送
function hhContentIdSubmit(data){
	result = '';
	$.ajax({
		url:'https://dd.youdnr.com/api/mate/list/store',
		type:"post",
		async:false,
		 data:{
			 data:data,
		 },
		success:function(data){
			console.log(data);
			result = {
				code: data.code,
				msg:data.msg
			}
		},
		error:function(){
			layer.msg('抛送异常');
		}
	})
	return result;
}
//content_id获取
function hhContentId(){
	result = '';
	$.ajax({
		url:'https://dd.youdnr.com/api/mate/contentid?platform=0',
		type:"get",
		async:false,
		success:function(data){
			if(data.result){
				result = data.result;
			}
		},
		error:function(){
			layer.msg('抛送异常');
		}
	})
	return result;
}

